package com.scut.online_black_market.utility;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.scut.online_black_market.dto.AlipayOrder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Random;
import java.util.UUID;

/**
 * create by yellowBroke on 2020年11月28日 14点30分
 * 单例模式创建阿里支付宝client
 */
@Component
public class AliPayClientUtil {

    private String APP_ID = "2016110300789353";

    private String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCmcXM8d8lxcD9xzQnDqSN80KJMIocKShb30uEzHrkO1JIHknpHuqQCC18Jpl+2uTjjnqbl8yTmSc0GBY6KvLtwBTpepsSyvdUBxSNlIxjkA5zFLxG+NYcAaQRgxy7c6dD11Akroxiq2b5im4AB83kkglbub9JqqeH+ZAnCX7YVceG/4Yc52ChyOLxTYhHi7HfThycWgm8S/D5YNx01mXeBIOoDleyhuqevNnHrRCvZZM/2kRPg+ya7Ka5vu7Jjwfuf7uRsaEW5tMZu0LCOknro1RGR2ghvndUCF3kuEduURhhcckHE2PRUW0RSwyj2dkXmIWzRGIl8cQ2pI5bvh1/vAgMBAAECggEAeO6bPMcIDSzyW/wO6SO+ThTEpy67EUXNtFh7GYpsCRN32pqZz/4S5g1fCwLgLkIbzH5cY10rZUsf9Q0aiUEhqv2RcG4ZIol896FF21kJ37mkMTzG1n3Lzgd/j9WI/ifAlKxv0U3aQWCdzMWQ11oQne85vFpw9uDh+9SxppGmGaONnC/5efts1enhb74IR+0yhmEo8VdEsbsXfBDj9SBKPSR5hWeEdyaMboB5m7ae6qimZCyaeIJZ7SI2zvvSC/5rCGB/EOQ8DoGmt+SFiWqRHimuLvoL1YhvunjqtR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OKkDi54guskaE3mBOEbIi5treuGvSmSbVylo2jIxiA2xYh6z5D3VTVeoZHWabULbN3Zfe/C3LWIQyFQM9YbjyOOOdsabrq4xC50h+I2KZukXR4iOE9lnSkcBX7aIAUitpR+Z0BBNP5icI5knVQUQ9pvRqF/akyPK5sheSrZhpMSt/CIPeDIHrtSYVj50IH2f/cG0XrTXYN5HrnpDS08VenBIIhUBxnYiU94rxEUEQHDwIxkhMh8P+7VLJZRmgZASMIqt8sKwmA3YwPQIDAQAB";


    private static AliPayClientUtil instance;
    private AlipayClient alipayClient;

    public static AliPayClientUtil getInstance() {
        if (instance == null){
            instance = new AliPayClientUtil();
        }
        return instance;
    }

    private AliPayClientUtil() {
        alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do",
                APP_ID,
                APP_PRIVATE_KEY,
                "json",
                "utf-8",
                ALIPAY_PUBLIC_KEY,
                "RSA2");
    }

    public String createTrade(float money, String uuid) throws AlipayApiException {

        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        //SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setBody(uuid);
        model.setSubject("用户充值");
        model.setOutTradeNo(uuid);
        model.setTimeoutExpress("30m");
        model.setTotalAmount(String.valueOf(money));
        model.setProductCode("QUICK_MSECURITY_PAY");
        request.setBizModel(model);
        request.setNotifyUrl("http://47.99.159.129:8090/aliPay/notify");
        request.setReturnUrl("http://47.99.159.129:8090/aliPay/returnUrl");

        //这里和普通的接口调用不同，使用的是sdkExecute
        AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
        return response.getBody();
    }
}
